//
//  ST_PairsShareParticularModel.swift
//  btvideo
//
//  Created by mac on 2019/12/30.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyUserDefaults
import SwiftyJSON

enum ST_PairsMotifType:Int {
    case image,video
}

let PairsShareHeaderMaxHeight = 500.cgFloat

class ST_PairsShareParticularModel: NSObject {
    
    var type = ST_PairsMotifType.image
    
    var videoUrl = ""
    
    var imageUrl = ""
    
    var width:CGFloat = 0
    
    var height:CGFloat = 0
    
    var duration = 0
    
    lazy var displayHeight:CGFloat = {
        
        let w = self.width < 1 ? 1 : self.width
        
        let h = ScreenWidth/w*height
        
        if h > PairsShareHeaderMaxHeight{
            return PairsShareHeaderMaxHeight
        }
        
        return h
    }()
    
    override init() {
        super.init()
    }
    
    /**
     从后台的数据结构解析为本身数据结构
     */
    init(json:JSON) {
        
        super.init()
        
//        类型(1:视频,2:图片)
        if  json["type"].intValue == 1{
            type = .video
        }

        if let url = json["video_url"].string,url.count > 3{
            videoUrl = url
        }
        
        imageUrl = json["content"].stringValue
        
        width = json["width"].floatValue.cgFloat
        
        height = json["height"].floatValue.cgFloat
        
        duration = json["duration"].intValue
    }
    
    
    /**
     通过image URL 初始化
     */
    init(imageUrl:String,width:CGFloat,height:CGFloat) {
        super.init()
        self.type = .image
        self.imageUrl = imageUrl
        self.width = width
        self.height = height
    }
    
    
    /**
     通过image&&video URL 初始化
     */
    init(imageUrl:String,videoUrl:String,width:CGFloat,height:CGFloat) {
        super.init()
        self.type = .video
        self.imageUrl = imageUrl
        self.videoUrl = videoUrl
        self.width = width
        self.height = height
    }
    
    
    static func describe(json_array:[JSON])->[ST_PairsShareParticularModel]{
        
      
        
        let array = json_array.map { (json) -> ST_PairsShareParticularModel in
            return ST_PairsShareParticularModel.init(json: json)
        }
        
        return array
    }
    
}
